package middlware;

import java.io.*;

import application.Message;

public class Marshaller {
	
	public Marshaller() {
	}

	public byte[] serializarObjeto(Message msg) throws Exception {

        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(byteStream); 
 
        oos.flush();
        oos.writeObject(msg); 
        oos.flush(); 

        byte[] sendBuf = byteStream.toByteArray(); 
        oos.close();
        return sendBuf;
    }
	
	public Message desserializarObjeto(byte[] mensagem) throws IOException, ClassNotFoundException {
            ByteArrayInputStream byteStream = new ByteArrayInputStream(mensagem);
            ObjectInputStream is = new ObjectInputStream(byteStream);
            Message obj = (Message)is.readObject();    

            is.close(); 
            return obj; 

	}	
}
